document.getElementById('back').addEventListener('click', function () {
    // window.history.back();
    location.href="popup.html"
});

document.getElementById('selectPath').addEventListener('click', function () {
    document.getElementById("selectPathView").style.display="block";
    //加载根目录
    selectPath("")
    document.getElementById("selectPathViewPath").dataMap = {};
    document.getElementById("selectPathViewPath").value = "";
});


//下载按钮
document.getElementById('download').addEventListener('click', function () {

    //判断是否选择了下载路径
    let downloadPath = document.getElementById("downloadPath").pathId;
    let downloadUrl = document.getElementById("downloadUrl").value.trim();

    if (!downloadPath) {
        foxui.message({
            type: 'danger', text: '请选择下载路径' });
        return;
    }

    //下载
    foxui.loading();
    http("/addTask", {
        id:downloadPath,
        url:downloadUrl
    },function (res) {
        foxui.closeLoading();
        foxui.message({
            type: 'success', text: '成功提交下载' });
        // window.history.back();

        location.href="popup.html"
    },{
        http(){foxui.closeLoading();},
        body(){foxui.closeLoading();}
    });
});



//路径选择器
document.getElementById("selectPathView").addEventListener('click', function () {
    document.getElementById("selectPathView").style.display="none";
});

//路径选择页面 返回上一级目录
document.getElementById('selectPathViewBack').addEventListener('click', function (event) {
    event.stopPropagation();
    delPath();
});

//路径选择页面 新目录按钮
document.getElementById('selectPathViewNew').addEventListener('click', function (event) {
    event.stopPropagation();

    if (!document.getElementById("selectPathViewPath").value.includes("/")) {
        foxui.message({
            type: 'danger',text: '请选择新文件夹的父路径' });
        return;
    }

    const name = prompt("在\n"+document.getElementById("selectPathViewPath").value+"\n下创建的新文件夹名称", "新文件夹"); //显示默认文本 "Keafmd"
    if (name!=null && name!==""){
        foxui.loading();
        let id = document.getElementById("selectPathViewPath").dataMap[document.getElementById("selectPathViewPath").value].id;
        http("/insertPath", {
            id: id,
            dir:name
        },function (res) {
            foxui.closeLoading();
            selectPath(id);
        },{
            http(){foxui.closeLoading();
                selectPath(id);},
            body(){foxui.closeLoading();
                selectPath(id);}
        });


    }else {
        foxui.message({
            type: 'danger',text: '输入有效文件夹名称' });
    }
});

//路径选择页面 确定按钮
document.getElementById('selectPathViewOk').addEventListener('click', function (event) {
    event.stopPropagation();
    if (!document.getElementById("selectPathViewPath").value.includes("/")) {
        foxui.message({
            type: 'danger', text: '请选择最少一级路径' });
        return;
    }
    document.getElementById("selectPathView").style.display="none"
    document.getElementById("downloadPath").value=document.getElementById("selectPathViewPath").value;
    document.getElementById("downloadPath").pathId=document.getElementById("selectPathViewPath").dataMap[document.getElementById("selectPathViewPath").value].id;
});

//删除一个路径
function delPath(){
    let elementById = document.getElementById("selectPathViewPath");
    let split = elementById.value.split('/');
    if (split.length == 1) {
        foxui.message({
            type: 'danger', text: '已经是根目录' });
        return;
    }
    var newPath = "";
    for (let i = 0; i < split.length-2; i++) {
        newPath = newPath +split[i]+"/";
    }

    let dataMapElement = document.getElementById("selectPathViewPath").dataMap[newPath];

    document.getElementById("selectPathViewPath").value=newPath;
    if (dataMapElement) {
        selectPath(dataMapElement.id)
    }else {
        selectPath("")
    }

}


function selectPath(id){
    foxui.loading();
    http("/selectPath", {
        id:id
    },function (res) {
        foxui.closeLoading();

        let elementById = document.getElementById("selectPathViewList");
        elementById.innerHTML="";

        let files = res.data.files;
        for (let i = 0; i < files.length; i++) {
            let file = files[i];

            elementById.insertAdjacentHTML('afterbegin'," <li id='"+file.id+"' class=\"foxui-menu-item \">\n" +
                "            <i class=\"foxui-menu-submenu-icon foxui-icon-mulu-f\"></i>\n" +
                "            <span>"+file.name+"</span>\n" +
                "          </li>");

            document.getElementById(file.id).addEventListener('click', function (event) {
                event.stopPropagation();
                selectPath(file.id)
                document.getElementById("selectPathViewPath").value=file.params.aliasPath;
                document.getElementById("selectPathViewPath").dataMap[file.params.aliasPath]=file;
            });

        }

    },{
        http(){foxui.closeLoading();},
        body(){foxui.closeLoading();}
    });
}


//获取js内传递的url
const searchParams = new URLSearchParams(window.location.search);
if (searchParams.get('id')) {
    let number = parseInt(searchParams.get('id'));
    foxui.dialog({
        title: '选择下载器',
        content: '选择你需要的下载方式',
        confirmText: 'IndSvr Download',
        cancelText: '浏览器下载',
        confirm: function (callback) {
            chrome.downloads.cancel(number);
            chrome.downloads.erase({id:number});
            document.getElementById("downloadUrl").value=decodeURIComponent(escape(atob(searchParams.get('url'))));
            callback();
        },
        cancel:function (callback){
            chrome.downloads.resume(number)
            window.close();
            callback();
        }
    });
}else {
    document.getElementById("downloadUrl").value=decodeURIComponent(escape(atob(searchParams.get('url'))));
}
